package com.wxcms.ctrl;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wxcms.service.SysUserService;

/**
 * 
 */

@Controller
public class LoginCtrl {

	@RequestMapping(value = "/login")
	public ModelAndView login(){
		ModelAndView mv = new ModelAndView("wxcms/login");
		return mv;
	}
	
	@RequestMapping(value = "/signin")
	public ModelAndView signin(String username, String passwd, HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		UsernamePasswordToken token = new UsernamePasswordToken(username, passwd, request.getRemoteHost());
		token.setRememberMe(true);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			if(subject.isAuthenticated()){
				mv.setViewName("redirect:/index.html");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mv.setViewName("redirect:/login");
		}
		return mv;
	}
	
	@RequestMapping("/logout")
	public Object logout(HttpServletRequest request, ModelMap modelMap) {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		ModelAndView mv = new ModelAndView("redirect:/login");
		return mv;
	}
}
